{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 绝对值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实数的绝对值\n",
    "实数绝对值严格的数学定义：一个数 x，如果 x 为正数,那么 $|x|=x$，如果x为0,那么$|x|=0$.如果 x 为负数，那么$|x|=-x$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://www.zhihu.com/equation?tex=%7B%5Cdisplaystyle%20%5Cleft%7Ca%5Cright%7C%3D%7B%5Cbegin%7Bcases%7Da%26(a%3E0)%5C%5C0%26(a%3D0)%5C%5C-a%26(a%3C0)%5Cend%7Bcases%7D%7D%7D)\n",
    "\n",
    "![实数绝对值的平面座标图](https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Absolute_value.svg/700px-Absolute_value.svg.png)\n",
    "\n",
    "实数绝对值的平面座标图\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算法则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "絕對值有以下性質：\n",
    "* $|a| \\ge 0$\n",
    "* 若 $a = 0$，则 $|a| = 0$\n",
    "* $|ab| = |a| \\times |b|$\n",
    "* 若 $b \\neq 0$，$|\\frac{a}{b}| = \\frac{|a|}{|b|}$\n",
    "* $|a + b| \\le |a| + |b|$  (三角不等式)\n",
    "* $|a - b| \\ge \\Big|\\big|a\\big| - \\big|b\\big|\\Big|$\n",
    "* 若 $a>0$，則$a = \\left | a \\right |$，若 $a<0$，則 $a = -|a|$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 微积分\n",
    "绝对值函数在 $x=0$ 处不可导。\n",
    "\n",
    "![](https://www.zhihu.com/equation?tex=%7B%5Cdisplaystyle%20%7B%5Cfrac%20%7Bd%7D%7Bdx%7D%7D%7Cx%7C%3D%7B%5Cfrac%20%7Bx%7D%7B%7Cx%7C%7D%7D%3D%7B%5Cbegin%7Bcases%7D1%26x%3E0%5C%5C-1%26x%3C0%5Cend%7Bcases%7D%7D%7D%0A%0A%0A)\n",
    "\n",
    "${\\displaystyle \\int |x|dx={\\frac {x|x|}{2}}+C}$\n",
    "\n",
    "其中 C 是积分常数。$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复数的绝对值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "暂时不讲， TODO"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "10\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 实数内的函数实现很简单，这里可以看下在复数范围内 Python 下的解（你可以 clone github 并使用工程中的 docker 调试代码）\n",
    "\n",
    "from sympy import *\n",
    "a = symbols('a')\n",
    "print(abs(10))\n",
    "print(abs(-10))\n",
    "print(abs(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{1 + \\pi^{2}}$"
      ],
      "text/plain": [
       "sqrt(1 + pi**2)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(1 + pi*I) # 复数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JS 实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "const abs = (x) => {\n",
    "  if (x > 0) return x\n",
    "  if (x === 0) return 0\n",
    "  if (x < 0) return -x\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "绝对值的概念比较简单，但在计算上比较讨厌，因为遇到绝对值的时候，要开始一个个分支计算，一个绝对值就将分出至少 2 条路，如果式子中有多个，就是指数次的分支增加了。在统计学上，我们会用方差或标准差来绕过讨厌的绝对值，这个之后再讲。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
